package com.example.demo.controller;

import com.example.demo.log.Log;
import com.example.demo.log.LogModuleEnum;
import com.example.demo.model.core.Add;
import com.example.demo.model.core.PageVo;
import com.example.demo.model.core.R;
import com.example.demo.model.core.Update;
import com.example.demo.model.form.RoleForm;
import com.example.demo.model.form.RolePermForm;
import com.example.demo.model.query.RoleQuery;
import com.example.demo.model.vo.OptionVO;
import com.example.demo.model.vo.RoleVO;
import com.example.demo.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/role")
@Tag(name = "角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('sys:role:add')")
    @Operation(description = "添加角色")
    @Log(value = "添加角色", module = LogModuleEnum.ROLE)
    public R<Void> add(@Validated(Add.class) @RequestBody RoleForm form) {
        sysRoleService.add(form);
        return R.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('sys:role:edit')")
    @Operation(description = "修改角色")
    @Log(value = "修改角色", module = LogModuleEnum.ROLE)
    public R<Void> update(@Validated(Update.class) @RequestBody RoleForm form) {
        sysRoleService.update(form);
        return R.ok();
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPerm('sys:role:delete')")
    @Operation(description = "删除角色")
    @Log(value = "删除角色", module = LogModuleEnum.ROLE)
    public R<Void> delete(@RequestBody List<String> ids) {
        sysRoleService.delete(ids);
        return R.ok();
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('sys:role:query')")
    @Operation(description = "角色分页列表")
    public R<PageVo<RoleVO>> page(@Validated RoleQuery query) {
        PageVo<RoleVO> page = sysRoleService.page(query);
        return R.ok(page);
    }

    @GetMapping("/form")
    @Operation(description = "角色表单")
    public R<RoleForm> form(@Parameter(description = "角色ID") String id) {
        RoleForm form = sysRoleService.form(id);
        return R.ok(form);
    }

    @GetMapping("/permIds")
    @Operation(description = "角色权限Id列表")
    public R<List<String>> getPermIds(@Parameter(description = "角色Id") @RequestParam String roleId) {
        List<String> menus = sysRoleService.getRolePermIds(roleId);
        return R.ok(menus);
    }

    @PostMapping("/perms")
    @PreAuthorize("@ss.hasPerm('sys:role:perms')")
    @Operation(description = "修改角色权限")
    @Log(value = "修改角色权限", module = LogModuleEnum.ROLE)
    public R<Void> updatePermIds(@Validated @RequestBody RolePermForm form) {
        sysRoleService.updateRolePerms(form);
        return R.ok();
    }

    @GetMapping("/options")
    @Operation(description = "角色下拉列表")
    public R<List<OptionVO<String>>> options(@Validated RoleQuery query) {
        List<OptionVO<String>> list = sysRoleService.options(query);
        return R.ok(list);
    }
}
